package jsyf.oa.service.doc;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import jsyf.oa.dao.doc.FileTypeDao;
import jsyf.oa.entity.doc.FileTypeEntity;
import jsyf.oa.facade.doc.FileTypeFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Maqiuqiu on 2018/4/24.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class FileTypeService implements FileTypeFacade{

    @Autowired
    private FileTypeDao fileTypeDao;

    @Override
    public String save(String jsonData) {
        FileTypeEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), FileTypeEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            rows = fileTypeDao.add(entity);
        } else {
            FileTypeEntity fileTypeEntity = (FileTypeEntity) fileTypeDao.getIBaseEntityById(entity.getId());
            fileTypeEntity.setColumnType(entity.getColumnType());
            fileTypeEntity.setDocumentName(entity.getDocumentName());
            fileTypeEntity.setCompanyId(entity.getCompanyId());
            fileTypeEntity.setAttachmentName(entity.getAttachmentName());
            fileTypeEntity.setAttachmentUrl(entity.getAttachmentUrl());
            fileTypeEntity.setPictureName(entity.getPictureName());
            fileTypeEntity.setPictureUrl(entity.getPictureUrl());
            fileTypeEntity.setScreenType(entity.getScreenType());
            fileTypeEntity.setDepartId(entity.getDepartId());
            fileTypeEntity.setCreateId(entity.getCreateId());
            fileTypeEntity.setCreateTime(entity.getCreateTime());
            rows = fileTypeDao.update(fileTypeEntity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public Boolean delById(String id) {
        return fileTypeDao.delById(id) > 0;
    }

    @Override
    public Boolean removeById(String jsonData) {
        return fileTypeDao.removeById(JSON.parseObject(jsonData).getString("id")) > 0;
    }
}
